<template>
  <div class="wrap">
    <div class="login">
      <el-card class="box-card">
        <h4 style="text-align:center">登录</h4>
        <br>
        <el-form :model="userInfo" status-icon :rules="rules" ref="ruleForm" label-width="50px" class="demo-ruleForm">
          <el-form-item label="账号" prop="username">
            <el-input v-model.number="userInfo.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="userInfo.password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item align="center">
            <el-button type="primary" @click="submitForm">提交</el-button>
            <el-button @click="userInfo={}">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import Mock from 'mockjs'
export default {
  name: "index",
  data(){
    return{
      userInfo:{
        username:'admin',
        password:'admin',
      },
      rules: {
        username: [
          { required:true, trigger: 'blur' }
        ],
        password: [
          { required:true, trigger: 'blur' }
        ],
      }
    }
  },
  methods:{
    submitForm(){
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let res = await this.$API.getMenu(this.userInfo)
          if (res.code === 20000){
            this.$message('登录成功')
            //存储token
            this.$store.commit('setToken',Mock.Random.guid())
            //存储menuList
            localStorage.setItem('menuList',JSON.stringify(res.data.menu|| []))
            await this.$router.push({
              path: '/'
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>
.login{
  top:50%;
  left:50%;
  position:absolute;
  transform:translate(-50%,-50%);
  /*margin:180px auto;*/
  /*width:350px;*/

}
</style>
